package com.isunland.managesystem.base;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.isunland.managesystem.R;

import static com.isunland.managesystem.R.id.toolbar;

/**
 * viewpager封装类
 *
 * @author wangyanan
 */
@SuppressWarnings("deprecation") public abstract class BasePagerActivity extends BaseVolleyActivity
    implements ActionBar.TabListener {
  public TabLayout getTabLayout() {
    return mTabLayout;
  }

  public AppBarLayout getAppBar() {
    return mAppBar;
  }

  TabLayout mTabLayout;
  AppBarLayout mAppBar;
  //private static final String TAG = "BasePagerActivity";
  private ViewPager mViewPager;
  private Fragment mFragmentOne;
  private Fragment mFragmentTwo;
  private Fragment mFragmentThree;
  private ActionBar actionBar;
  private int[] mTitleId;
  private int mSize;
  private boolean isFixedTitle;// 是否固定标题
  private String mFixedTitle;// 固定的标题内容.

  /**
   * 第一个Fragment
   */
  protected abstract Fragment createFragmentOne();

  /**
   * 第二个Fragment
   */
  protected abstract Fragment createFragmentTwo();

  /**
   * 第三个Fragment. 因为一些界面只需要2个Fragment切换,这个方法不是必须实现的.
   */
  public Fragment createFragmentThree() {
    return null;
  }

  /**
   * 标题资源字符串的id.
   */
  protected abstract int[] createTitle();

  @Override protected void onCreate(Bundle arg0) {

    //mViewPager = new ViewPager(this);
    //mViewPager.setId(R.id.viewPager);
    setContentView(getLayoutResId());
    mViewPager = (ViewPager) findViewById(R.id.viewPager);
    Toolbar mToolBar = (Toolbar) findViewById(toolbar);
    if (mToolBar != null) {

      mToolBar.setTitle(R.string.app_name);
      mToolBar.setTitleTextColor(getResources().getColor(R.color.white));

      mToolBar.setOverflowIcon(getResources().getDrawable(R.drawable.ic_more_vert_white));
      mToolBar.setNavigationIcon(R.drawable.ic_arrow_back_white);
    }

    //mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
    //    @Override
    //    public void onClick(View view) {
    //        onBackPressed();
    //    }
    //});
    //        if (Build.VERSION.SDK_INT >= 19) {
    //            if (isOverLay()) {
    //
    //                View layout = findViewById(R.id.appbar);
    //                MyUtils.setPaddingTop(layout, MyUtils.getStatusBarHeight(this));
    //            } else {
    //                View root = findViewById(R.id.container_root);
    //                //root.setBackgroundResource(R.color.primary);
    //                Window window = getWindow();
    //                //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
    //                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    //                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    //                    //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
    //                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    //                    //设置状态栏颜色
    //                    window.setStatusBarColor(getResources().getColor(R.color.primary));
    //                }
    //                MyUtils.setFitWindow(root);
    //            }
    //        }

    setSupportActionBar(mToolBar);

    actionBar = getSupportActionBar();
    mTitleId = createTitle();

    mSize = mTitleId.length;
    // 默认不固定标题,如果固定的话这个方法会被重写.
    setFixedTitle(false, "");
    if (isFixedTitle) {
      // 标题不变
      actionBar.setTitle(mFixedTitle);
    } else {
      // 标题变化使用变化标题.
      actionBar.setTitle(mTitleId[0]);
    }
    //actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    //for (int i = 0; i < mSize; i++) {
    //    actionBar.addTab(actionBar.newTab().setText(mTitleId[i])
    //            .setTabListener(this));
    //}
    super.onCreate(arg0);
    FragmentManager fm = getSupportFragmentManager();
    mFragmentOne = createFragmentOne();
    mFragmentTwo = createFragmentTwo();
    mFragmentThree = createFragmentThree();
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {

      @Override public int getCount() {
        return mSize;
      }

      @Override public Fragment getItem(int arg0) {
        switch (arg0) {
          case 0:
            return mFragmentOne;
          case 1:

            return mFragmentTwo;
          case 2:

            return mFragmentThree;

          default:
            return null;
        }
      }

      @Override public CharSequence getPageTitle(int position) {
        return getString(mTitleId[position]);
      }
    });

    mTabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
    mAppBar = (AppBarLayout) findViewById(R.id.appbar);

    if (mTabLayout == null) {
      super.onCreate(arg0);
      return;
    }
    for (int i = 0; i < mSize; i++) {
      mTabLayout.addTab(mTabLayout.newTab().setText(mTitleId[i]));
    }

    mTabLayout.setupWithViewPager(mViewPager);
    mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
      @Override public void onTabSelected(TabLayout.Tab tab) {
        setNavigateTo(tab.getPosition());
      }

      @Override public void onTabUnselected(TabLayout.Tab tab) {

      }

      @Override public void onTabReselected(TabLayout.Tab tab) {

      }
    });

    setNavigateTo(currentTab());
  }

  protected int getLayoutResId() {
    return R.layout.activity_viewpager;
  }

  /**
   * 是否固定标题,默认不固定,随着下面tab页轮换.
   *
   * @param isFixedTitle 是否固定标题
   * @param fixedTitle 固定标题.
   */
  protected void setFixedTitle(boolean isFixedTitle, String fixedTitle) {
    if (null == fixedTitle) {
      mFixedTitle = "";
    } else {
      mFixedTitle = fixedTitle;
    }
    this.isFixedTitle = isFixedTitle;
  }

  /**
   * 跳转页面。
   *
   * @param position 从0开始
   */
  public void setNavigateTo(int position) {
    mViewPager.setCurrentItem(position);
    //actionBar.setSelectedNavigationItem(position);
    if (!isFixedTitle) {
      // 不固定标题,可以切换.
      actionBar.setTitle(getResources().getString(mTitleId[position]));
    }
  }

  public int currentTab() {
    return 0;
  }

  @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    mViewPager.setCurrentItem(tab.getPosition());
  }

  @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

  }

  @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

  }

  /**
   * 是否遮挡内容
   */
  //    protected boolean isOverLay() {
  //        return true;
  //    }
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        finish();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }
}
